package dao.user;

import java.sql.*;
import java.util.*;


import entity.User;
import utils.ComonUtil;
import utils.SqlConnection;

/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 
 *
 */
public class UserDaoImpl implements UserDao {

	@Override
	public List<User> getUserList(Connection connection, User user) throws Exception {
		PreparedStatement pstm = null;
        ResultSet res = null;
        List<User> userList = new ArrayList<>();
        if (connection != null) {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("select * from user  ");
            List<Object> paramList = new ArrayList<>();

			if (!ComonUtil.isEmpty(user.getId())) {
				paramList.add(user.getId());
				stringBuffer.append("and id = ? ");
			}
            //密码
            if (!ComonUtil.isEmpty(user.getPassword())) {
                System.out.println("获取道密码：" + user.getPassword());
                paramList.add(user.getPassword());
                stringBuffer.append("and password = ? ");//模糊查询
            }
            //身份
            if (!ComonUtil.isEmpty(user.getType())) {
                System.out.println("获取道身份：" + user.getType());
                paramList.add(user.getType());
                stringBuffer.append("and type = ? ");//模糊查询
            }
            //用户名是否为空或null
            if (!ComonUtil.isEmpty(user.getUserName())) {
                System.out.println("获取道姓名：" + user.getUserName());
                paramList.add("%" + user.getUserName() + "%");
                stringBuffer.append("and userName like ? ");//模糊查询
            }
      
         
            Object[] params = paramList.toArray();
            String sql = stringBuffer.toString().replaceFirst("and", "where");
            System.out.println("用户查询---"+sql);
            res = SqlConnection.executeQuery(connection, pstm, res, sql, params);
            while (res.next()) {
                User user1 = new User();
                user1.setId(res.getString("id"));
                user1.setUserName(res.getString("userName"));
                user1.setPassword(res.getString("password"));
                user1.setType(res.getString("type"));
                user1.setAdmin(res.getInt("admin"));
                userList.add(user1);
            }
            SqlConnection.closeConnection(connection, pstm, res);
        }
        return userList;
	}

	@Override
	public int addUser(Connection con, User user) throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement pstm = null;
		int row = 0;
		
		if(con!=null) {
			String sql = "insert into user values(?,?,?,?,?)";
			Object[] params = {
					user.getId(),
					user.getUserName(),
					user.getPassword(),
					user.getAdmin(),
					user.getType()
			};
			
			con = SqlConnection.getConnection();
			row = SqlConnection.executeUpdate(con, pstm, sql, params);
			SqlConnection.closeConnection(con, pstm, null);
		}
		
		return row;
	}

	@Override
	public int deleteUser(Connection con, String id) throws Exception {
		// TODO Auto-generated method stub
		
		PreparedStatement pstm = null;
		int row = 0 ;
		if(con!=null) {
			String sql = "delete from user where id = ?";
			Object[] params = {id};
			row = SqlConnection.executeUpdate(con, pstm, sql, params);
		}
		return row;
	}

	@Override
	public int upUser(Connection con, User user) throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement pstm =null;
		int row = 0;
		
		if(con!=null) {
			StringBuffer stringBuffer = new StringBuffer();
			stringBuffer.append("update user set ");
			ArrayList<Object> paramsList = new ArrayList<Object>();
			if(!ComonUtil.isEmpty(user.getUserName())) {
				stringBuffer.append("userName = ?,");
				paramsList.add(user.getUserName());
			}
			if(!ComonUtil.isEmpty(user.getPassword())) {
				stringBuffer.append("password = ?,");
				paramsList.add(user.getPassword());
			}
			if(!ComonUtil.isEmpty(user.getType())) {
				stringBuffer.append("type = ?,");
				paramsList.add(user.getType());
			}
			if(user.getAdmin()!=-1) {//-1表示不修改
				stringBuffer.append("admin = ?,");
				paramsList.add(user.getAdmin());
			}
			
			String sql = stringBuffer.toString();
//			System.out.println(sql.substring(sql.length()-1));
			if(sql.substring(sql.length()-1).equals(",")) {
				sql = sql.substring(0,sql.length()-1);
			}
			sql+=" where id = ?";
			paramsList.add(user.getId());
//			System.out.println("修改语句："+sql);
			
			Object[] params = paramsList.toArray();
			row = SqlConnection.executeUpdate(con, pstm, sql, params);
			SqlConnection.closeConnection(con, pstm, null);
		}
		
		
		return row;
	}

}
